﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Domain.Common;
using Domain.Map;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MapComponentSprite
{
    /// <summary>
    /// 建筑物类型
    /// </summary>
    public class BuildingTypeSprite:AutomatedSprite
    {
        /// <summary>
        /// 建筑物类型缓存
        /// </summary>
        public static Dictionary<string, Texture2D> BuildingTypes = new Dictionary<string, Texture2D>();
        public override void Draw(GameTime gameTime)
        {
            if (this.Image != null)//解决地图跳转功能，图像为空异常
            SpriteBatch.Draw(Image, DrawPosition, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.8f);
        }

        public void Update(GameTime gameTime, string cityTypeId, int cityLevel, Point position)
        {
            this.Image = LoadTypePic(cityTypeId, cityLevel);
            int p=0, b=1;//坐标调节参数 <5  默认值为0
            if (cityLevel >= 5 && cityLevel < 13)
            {
                p = 1;
                b = 3;
            }
            else if (cityLevel >= 13)
            {
                p = 2;
                b = 5;
            }
            Rectangle rectangle = new Rectangle();
            rectangle.X = (position.X-p) * MapUnit.Width + GameScreen.LeftEdge;
            rectangle.Y = (position.Y-p) * MapUnit.Height + GameScreen.TopEdge;
            rectangle.Width = MapUnit.Width*b;
            rectangle.Height = MapUnit.Height*b;
            DrawPosition = rectangle;
        
        }
        /// <summary>
        /// 获取城池等级对应的图片[0,5),[5,13),[13,...)
        /// </summary>
        /// <param name="cityTypeId"></param>
        /// <param name="cityLevel"></param>
        /// <returns></returns>
        private Texture2D LoadTypePic(string cityTypeId, int cityLevel)
        {
            string key = "";
            if (cityLevel < 5)
                key = cityTypeId;
            else if (cityLevel >= 5 && cityLevel < 13)
                key = cityTypeId + "-5";
            else if (cityLevel >= 13)
                key = cityTypeId + "-13";
            if (BuildingTypes.ContainsKey(key))
                return BuildingTypes[key];
            Texture2D texture2D = Sprite.Game.Content.Load<Texture2D>(@"Images\buildings\type\" + key);
            if (texture2D != null)
                BuildingTypes.Add(key, texture2D);
            return texture2D;
        }
    }
}
